package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ItemBankDO;
import com.jinzhi.jzweb.domain.ItemBankResourcesDO;
import com.jinzhi.jzweb.service.ItemBankResourcesService;
import com.jinzhi.jzweb.service.ItemBankService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 题库资源表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/itemBankResources")
public class ItemBankResourcesController extends BaseController {
	@Autowired
	private ItemBankResourcesService itemBankResourcesService;

	@Autowired
	private ItemBankService itemBankService;
	@Autowired
	private UserService userService;

	@GetMapping("/{id}")
	@RequiresPermissions("jzweb:itemBankResources:itemBankResources")
	String ItemBankResources(@PathVariable("id") Long id,Model model){
			model.addAttribute("ibankId",id);
	    return "jzweb/itemBankResources/itemBankResources";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:itemBankResources:itemBankResources")
	public Result<Page<ItemBankResourcesDO>> list(ItemBankResourcesDO itemBankResourcesDTO){
		// 查询列表数据
		Page<ItemBankResourcesDO> page = itemBankResourcesService.selectPage(getPage(ItemBankResourcesDO.class),
				itemBankResourcesService.convertToEntityWrapper("ibank_id", itemBankResourcesDTO.getIbankId(),
						"file_name", itemBankResourcesDTO.getFileName()).orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ItemBankResourcesDO obj = (ItemBankResourcesDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null){
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}
			}
			if (obj.getIbankId() != null){
				ItemBankDO itemBankDO = itemBankService.selectById(obj.getIbankId());
				if (itemBankDO != null) {
					obj.setIbankContent(itemBankDO.getTopicContent());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:itemBankResources:add")
	String add(Model model,String pid){
		model.addAttribute("pid",pid);
		ItemBankDO itemBankDO = itemBankService.selectById(pid);
		if(itemBankDO != null){
			model.addAttribute("name",itemBankDO.getTopicContent());
		}else{
			model.addAttribute("name",null);
		}
	    return "jzweb/itemBankResources/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:itemBankResources:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ItemBankResourcesDO itemBankResources = itemBankResourcesService.selectById(id);
		ItemBankDO itemBankDO = itemBankService.selectById(itemBankResources.getIbankId());
		if(itemBankDO != null){
			model.addAttribute("name",itemBankDO.getTopicContent());
		}else{
			model.addAttribute("name",null);
		}
		model.addAttribute("itemBankResources", itemBankResources);
	    return "jzweb/itemBankResources/edit";
	}
	
	@Log("添加题库资源表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:itemBankResources:add")
	public Result<String> save( ItemBankResourcesDO itemBankResources){
		itemBankResources.setCreaterId(this.getUserId());
		itemBankResourcesService.insert(itemBankResources);
        return Result.ok();
	}
	
	@Log("修改题库资源表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:itemBankResources:edit")
	public Result<String>  update( ItemBankResourcesDO itemBankResources){
		boolean update = itemBankResourcesService.updateById(itemBankResources);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除题库资源表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:itemBankResources:remove")
	public Result<String>  remove( Long id){
		itemBankResourcesService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除题库资源表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:itemBankResources:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		itemBankResourcesService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
